package com.guigu.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.common.utils.R;
import com.guigu.eduservice.entity.MyCollection;
import com.guigu.eduservice.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 收藏表（存储用户收藏的设备信息） 前端控制器
 * </p>
 *
 * @author Xiaosong
 * @since 2025-10-20
 */
@RestController
@RequestMapping("/advertisement/collection")
public class CollectionController {
    @Autowired
    private CollectionService collectionService;
    //添加收藏
    @PostMapping("/addCollection")
    public R addCollection(@RequestBody MyCollection myCollection){
        System.out.println("myCollection"+ myCollection);
        collectionService.save(myCollection);
        return R.ok();
    }
    //判断是否收藏
    @PostMapping("/isCollection")
    public R isCollection(@RequestBody MyCollection myCollection){
        QueryWrapper<MyCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",myCollection.getUserId());
        queryWrapper.eq("equipment_id",myCollection.getEquipmentId());
        MyCollection one = collectionService.getOne(queryWrapper);
        return one==null?R.error().message("未收藏"):R.ok().message("已收藏");
    }
    //取消收藏
    @PostMapping("/cancelCollection")
    public R cancelCollection(@RequestBody MyCollection myCollection){
        QueryWrapper<MyCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",myCollection.getUserId());
        queryWrapper.eq("equipment_id",myCollection.getEquipmentId());
        return collectionService.remove(queryWrapper)?R.ok().message("取消收藏成功"):R.error().message("取消收藏失败");
    }
}
